//
//  LXHistoryViewController.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LXHistoryViewController.h"
#import "CoreDataManager.h"
#import "LXHistoryModel.h"
#import "LXHistoryTableViewCell.h"
@interface LXHistoryViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *allArray;
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)UIAlertController *deleteAll;
@property(nonatomic,retain)NSManagedObjectContext *managerObjectContext;
@end

@implementation LXHistoryViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self selectAll];
    [self.myTableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _managerObjectContext = [[CoreDataManager shareManager]managedObjectContext];
    self.allArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor orangeColor];
    [self createNavigatetion];
    [self createTableView];
    [self createAlert];
}
-(void)createAlert{
    self.deleteAll = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否要清空所有的播放历史?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self deleteALL];
        [self selectAll];
        [self.myTableView reloadData];
    }];
    UIAlertAction *no = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [self.deleteAll addAction:no];
    [self.deleteAll addAction:ok];
}
-(void)deleteALL{
    for (MusicDB *temp in _allArray) {
         [_managerObjectContext deleteObject:temp];
    }
    [[CoreDataManager shareManager]saveContext];
}
-(void)createNavigatetion{
    self.navigationItem.title = self.naviTitle;
}
-(void)createTableView{
    self.myTableView = [[UITableView alloc]init];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        if ([self.naviTitle isEqualToString:@"播放历史"]) {
            make.bottom.mas_equalTo(0);
        }else{
        make.bottom.mas_equalTo(-49);
        }
    }];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.myTableView registerClass:[LXHistoryTableViewCell class] forCellReuseIdentifier:@"cell"];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]init];
    headView.backgroundColor = [UIColor colorWithWhite:0.885 alpha:1.000];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
    btn.frame = CGRectMake(SCREEN_WIDTH/5, 10, SCREEN_WIDTH/5, 15);
    [btn setBackgroundImage:[UIImage imageNamed:@"btn_select_listened_n@2x.png"] forState:UIControlStateNormal];
    UIButton *btnAll = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnAll addTarget:self action:@selector(btnAllAction) forControlEvents:UIControlEventTouchUpInside];
    btnAll.frame = CGRectMake(SCREEN_WIDTH*3/5, 10, SCREEN_WIDTH/5, 15);
    [btnAll setBackgroundImage:[UIImage imageNamed:@"btn_downloadsound_clear_n@2x.png"] forState:UIControlStateNormal];
    [headView addSubview:btnAll];
    [headView addSubview:btn];
    return headView;
}
-(void)btnAction{
    
}
-(void)btnAllAction{
    [self presentViewController:self.deleteAll animated:YES completion:^{
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _allArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LXHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.cellModel = [_allArray objectAtIndex:indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(void)selectAll{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MusicDB" inManagedObjectContext:_managerObjectContext];
    [fetchRequest setEntity:entity];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error = nil;
    NSArray *fetchedObjects = [_managerObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"%@",error);
    }
    self.allArray = [NSMutableArray arrayWithArray:fetchedObjects];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
